/**
 * 
 */
package com.jy.adscm.spss.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import com.baomidou.mybatisplus.service.IService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.spss.bean.Assemble;
import com.jy.adscm.spss.mapper.mysql.AssembleMapper;
import com.jy.adscm.spss.service.AssembleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * @author 刘中斐
 *@date 2018年8月11日 上午10:02:02 
 */
@Service
public class AssembleServiceImpl extends ServiceImpl<AssembleMapper,Assemble> implements AssembleService {

	@Autowired
	AssembleMapper assembleMapper;
//	@Autowired
//	HydropowerMapper hydropowerMapper;
	@Override
	public Map<String, Object> getByWokeEffect(String TASKCODE) throws ParseException {
		List<Assemble> effect = assembleMapper.getByWokeEffect(TASKCODE);
		List<Assemble> batchnums = assembleMapper.getBATCHNUMS(TASKCODE);
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		double AmWorkDiff=0;
		double PmWorkDiff=0;
		String amStartTime=null;
		String amEndTime=null;
		String pmStartTime=null;
		Map<String, Object> map=new HashMap<String, Object>();
		
		String pmEndTime=null;
		for (Assemble assemble : batchnums) {
			List<Assemble> list =new ArrayList<Assemble>();			
			for (Assemble ass : effect) {
				Assemble as = new Assemble(); 				
				if (assemble.getBATCHNUMS().equals(ass.getBATCHNUMS())) {
					if (ass.getAmStartTime()!=null&&ass.getAmEndTime()!=null) {
						amStartTime=ass.getAmStartTime();
						amEndTime=ass.getAmEndTime();
						long workTime=sd.parse(ass.getAmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime();
						AmWorkDiff=workTime/1000/60;
					}else if(ass.getAmStartTime()!=null&&ass.getAmEndTime()==null&&ass.getPmStartTime()==null&&ass.getPmEndTime()!=null){
						amStartTime=ass.getAmStartTime();
						amEndTime=ass.getPmEndTime();
						//long workTime=(sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getPmStartTime()).getTime())+(sd.parse(ass.getAmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime());
						long workTime=sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime();
						AmWorkDiff=workTime/1000/60;
					}else if (ass.getAmStartTime()!=null&&ass.getAmEndTime()==null&&ass.getPmStartTime()!=null) {
						amStartTime=ass.getAmStartTime();						
						ass.setAmEndTime(ass.getAmStartTime().substring(0, 10)+"12:00:00");
						long workTime=sd.parse(ass.getAmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime();
						AmWorkDiff=workTime/1000/60;
						amEndTime=ass.getAmEndTime();
					}

					if (ass.getAmOperator()!=null) {
						as.setAmOperator(ass.getAmOperator());
					}

					if ("".equals(ass.getAmNotQualifiedNums())||ass.getAmNotQualifiedNums()==null) {
						ass.setAmNotQualifiedNums("0");
					}
					if ("".equals(ass.getAmQualifiedNums())||ass.getAmQualifiedNums()==null) {
						ass.setAmQualifiedNums("0");
					}
					as.setAmNotQualifiedNums(ass.getAmNotQualifiedNums());

					as.setAmQualifiedNums(ass.getAmQualifiedNums());
					if ("0".equals(ass.getAmQualifiedNums())) {
						as.setAmWorkDiff(0.0);
					}else {
						as.setAmWorkDiff(AmWorkDiff/(Double.valueOf(ass.getAmNotQualifiedNums())+Double.valueOf(ass.getAmQualifiedNums())));
					}
					as.setAmWorkTaking(AmWorkDiff);
					as.setAmEndTime(amEndTime);
					as.setAmStartTime(amStartTime);
					if (ass.getPmStartTime()!=null&&ass.getPmEndTime()!=null) {
						pmStartTime=ass.getPmStartTime();
						pmEndTime=ass.getPmEndTime();
						long workTime=sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getPmStartTime()).getTime();
						PmWorkDiff=workTime/1000/60;
					}else if (ass.getPmStartTime()!=null&&ass.getPmEndTime()==null) {
						pmStartTime=ass.getPmStartTime();
						ass.setPmEndTime(ass.getPmStartTime().substring(0, 10)+"17:30:00");
						long workTime=sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getPmStartTime()).getTime();
						PmWorkDiff=workTime/1000/60;
						pmEndTime=ass.getPmEndTime();
					}

					if (ass.getPmOperator()!=null) {
						as.setPmOperator(ass.getPmOperator());
					}
					as.setPmNotQualifiedNums(ass.getPmNotQualifiedNums());			
					as.setPmQualifiedNums(ass.getPmQualifiedNums());
					if ("".equals(ass.getPmNotQualifiedNums())||ass.getPmQualifiedNums()==null) {
						ass.setPmNotQualifiedNums("0");
					}
					if ("".equals(ass.getPmQualifiedNums())||ass.getPmNotQualifiedNums()==null) {
						ass.setPmQualifiedNums("0");
					}
					if (!"".equals(ass.getPmNotQualifiedNums())&&!"".equals(ass.getPmQualifiedNums())) {
						as.setPmWorkDiff(PmWorkDiff/(Double.valueOf(ass.getPmNotQualifiedNums())+Double.valueOf(ass.getPmQualifiedNums())));
					}
					if ("0".equals(ass.getPmQualifiedNums())) {
						as.setPmWorkDiff(0.0);
					}
					as.setPmWorkTaking(PmWorkDiff);
					as.setPmStartTime(pmStartTime);
					as.setPmEndTime(pmEndTime);
					as.setBATCHNUMS(ass.getBATCHNUMS().toUpperCase());
					as.setDETAILNAME(ass.getDETAILNAME());
					if (ass.getAmStartTime()!=null&&ass.getAmEndTime()!=null) {
						if (ass.getAmQualifiedNums()==null) {
							if (!"".equals(ass.getPmStartTime())&&!"".equals(ass.getPmEndTime())) {
								if (!"".equals(ass.getPmQualifiedNums())) {
									if ("".equals(ass.getPmNotQualifiedNums())) {
										ass.setPmNotQualifiedNums("0");
									}
									long worktime=(sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getPmStartTime()).getTime())+(sd.parse(ass.getAmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime());
									int time=(int) (worktime/1000/60);
									as.setTotalTaking(time/(Double.valueOf(ass.getPmNotQualifiedNums())+Double.valueOf(ass.getPmQualifiedNums())));
								}

							}

						}

					}

				}
				if (as.getAmNotQualifiedNums()!=null) {
					list.add(as);
				}
			
			}
			/*Map<String, Object> map2=new HashMap<String, Object>();
			if (!"".equals(assemble.getAmOperator())) {
				System.out.println("111111111111111");
				map2.put(assemble.getAmOperator(), list);
			}
			if (!"".equals(assemble.getPmOperator()))  {
				System.out.println("22222222222222222222");
				map2.put(assemble.getPmOperator(), list);
			}*/
			
			map.put(assemble.getBATCHNUMS(), list);

		}
		return map;
	}

	@Override
	public Map<String, Object> getSameBatch(String TASKCODE,String PLANUNIT,String other) throws Exception {
		List<Assemble> sameBatch = assembleMapper.getSameBatch(TASKCODE,PLANUNIT,other);
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		double AmWorkDiff=0;
		double PmWorkDiff=0;
		String amStartTime=null;
		String amEndTime=null;
		String pmStartTime=null;
		String pmEndTime=null;
		Map<String, Object> map=new HashMap<String, Object>();
		List<Assemble> list =new ArrayList<Assemble>();		
		for (Assemble ass : sameBatch) {
			Assemble as = new Assemble(); 		
			if (ass.getAmStartTime()!=null&&ass.getAmEndTime()!=null) {
				amStartTime=ass.getAmStartTime();
				amEndTime=ass.getAmEndTime();
				long workTime=sd.parse(ass.getAmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime();
				AmWorkDiff=workTime/1000/60;
			}else if(ass.getAmStartTime()!=null&&ass.getAmEndTime()==null&&ass.getPmStartTime()==null&&ass.getPmEndTime()!=null){
				amStartTime=ass.getAmStartTime();
				amEndTime=ass.getPmEndTime();
				long workTime=sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime();
				AmWorkDiff=workTime/1000/60;
			}else if (ass.getAmStartTime()!=null&&ass.getAmEndTime()==null&&ass.getPmStartTime()!=null) {
				amStartTime=ass.getAmStartTime();						
				ass.setAmEndTime(ass.getAmStartTime().substring(0, 10)+"12:00:00");
				long workTime=sd.parse(ass.getAmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime();
				AmWorkDiff=workTime/1000/60;
				amEndTime=ass.getAmEndTime();
			}
			as.setAmNotQualifiedNums(ass.getAmNotQualifiedNums());		
			as.setAmQualifiedNums(ass.getAmQualifiedNums());
			if ("".equals(ass.getAmNotQualifiedNums())||ass.getAmNotQualifiedNums()==null) {
				ass.setAmNotQualifiedNums("0");
			}
			if ("".equals(ass.getAmQualifiedNums())||ass.getAmQualifiedNums()==null) {
				ass.setAmQualifiedNums("0");
			}
			if ("".equals(ass.getPmNotQualifiedNums())||ass.getPmQualifiedNums()==null) {
				ass.setPmNotQualifiedNums("0");
			}
			if ("".equals(ass.getPmQualifiedNums())||ass.getPmNotQualifiedNums()==null) {
				ass.setPmQualifiedNums("0");
			}
			if ("0".equals(ass.getAmQualifiedNums())) {
				as.setAmWorkDiff(0.0);
			}else {
				as.setAmWorkDiff(AmWorkDiff/(Double.valueOf(ass.getAmNotQualifiedNums())+Double.valueOf(ass.getAmQualifiedNums())));
			}
			System.out.println(as.getAmWorkDiff()+"________________");
			
			/*if (ass.getAmEndTime()!=null&&ass.getAmStartTime()!=null&&ass.getPmStartTime()==null&&ass.getPmEndTime()==null) {
				if ("0".equals(ass.getAmQualifiedNums())&&!"".equals(ass.getPmQualifiedNums())&&ass.getPmQualifiedNums()!=null) {
					as.setAmWorkDiff(AmWorkDiff/(Double.valueOf(ass.getPmNotQualifiedNums())+Double.valueOf(ass.getPmQualifiedNums())));
				}
			}	*/
			
			as.setAmEndTime(amEndTime);
			as.setAmStartTime(amStartTime);
			if (ass.getPmStartTime()!=null&&ass.getPmEndTime()!=null) {
				pmStartTime=ass.getPmStartTime();
				pmEndTime=ass.getPmEndTime();
				long workTime=sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getPmStartTime()).getTime();
				PmWorkDiff=workTime/1000/60;
			}else if (ass.getPmStartTime()!=null&&ass.getPmEndTime()==null) {
				pmStartTime=ass.getPmStartTime();
				ass.setPmEndTime(ass.getPmStartTime().substring(0, 10)+"17:30:00");
				long workTime=sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getPmStartTime()).getTime();
				PmWorkDiff=workTime/1000/60;
				pmEndTime=ass.getPmEndTime();
			}
			as.setPmNotQualifiedNums(ass.getPmNotQualifiedNums());
			as.setPmQualifiedNums(ass.getPmQualifiedNums());
			as.setPmStartTime(pmStartTime);
			as.setPmEndTime(pmEndTime);
			as.setAmWorkTaking(AmWorkDiff);
			as.setPmWorkTaking(PmWorkDiff);
			as.setAmOperator(ass.getAmOperator());
			as.setPmOperator(ass.getPmOperator());
			if (ass.getAmStartTime()!=null&&ass.getAmEndTime()!=null) {
				if (ass.getAmQualifiedNums()!=null||!"".equals(ass.getAmQualifiedNums())) {
					as.setPmWorkDiff(PmWorkDiff/(Double.valueOf(ass.getPmNotQualifiedNums())+Double.valueOf(ass.getPmQualifiedNums())));
				}else {
					double worktime=AmWorkDiff+PmWorkDiff;
					as.setPmWorkDiff(worktime/(Double.valueOf(ass.getPmNotQualifiedNums())+Double.valueOf(ass.getPmQualifiedNums())));
				}
			}
			
			if ("0".equals(ass.getPmQualifiedNums())) {
				as.setPmWorkDiff(0.0);
			}
			
			if (ass.getAmStartTime()!=null) {
				if (ass.getAmQualifiedNums()==null) {
					if (!"".equals(ass.getPmStartTime())&&!"".equals(ass.getPmEndTime())) {
						if (!"".equals(ass.getPmQualifiedNums())) {
							long worktime=(sd.parse(ass.getPmEndTime()).getTime()-sd.parse(ass.getPmStartTime()).getTime())+(sd.parse(ass.getAmEndTime()).getTime()-sd.parse(ass.getAmStartTime()).getTime());
							double time=(worktime/1000/60);
							as.setTotalTaking(time/(Double.valueOf(ass.getPmNotQualifiedNums())+Double.valueOf(ass.getPmQualifiedNums())));
						}

					}

				}

			}
			if (as.getAmNotQualifiedNums()!=null) {
				list.add(as);
			}
		}
		map.put("result", list);
		return map;
	}

	@Override
	public List<Assemble> getNumber(String BATCHNUMS) {

		return assembleMapper.getBATCHNUMS(BATCHNUMS);
	}


	@Override
	public List<String> getTranslate() {

		return assembleMapper.getTranslate();
	}


	@Override
	public Set<String> getOperator(String BATCHNUMS) {
		List<String> am=assembleMapper.getAmOperator(BATCHNUMS);
		List<String> pm=assembleMapper.getPmOperator(BATCHNUMS);
		Set<String> set=new HashSet<String>();
		for (String string : am) {
			set.add(string);
		}
		for (String string : pm) {
			set.add(string);
		}
		return set;
	}


	/*@Override
	public List<Object> gethyDropPower() {
		SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmssms");
		String uuid= new String(UUID.randomUUID().toString());
		String code=sd.format(new Date());
		hydropower selectByPrimaryKey = hydropowerMapper.selectByPrimaryKey("f63e5d97-cdaa-4d93-a30a-65dd5ff2ac9d");
		//hydropowerMapper.insertdate(uuid,code);
		System.out.println(selectByPrimaryKey);
		return null;
	}*/


}
